package com.health.service;

import com.health.dto.ArticleRequest;
import com.health.dto.ArticlePageRequest;
import com.health.dto.PageResponse;
import com.health.model.Article;
import com.health.model.ArticleStatus;
import com.health.model.User;
import com.health.repository.ArticleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;

@Service
public class ArticleService {
    
    @Autowired
    private ArticleRepository articleRepository;
    
    public PageResponse<Article> getArticleList(ArticlePageRequest request) {
        Pageable pageable = PageRequest.of(
            request.getPage() - 1,
            request.getSize(),
            Sort.by(Sort.Direction.DESC, "createTime")
        );
        
        Specification<Article> spec = (root, query, cb) -> {
            List<Predicate> predicates = new ArrayList<>();
            
            if (StringUtils.hasText(request.getTitle())) {
                predicates.add(cb.like(root.get("title"), "%" + request.getTitle() + "%"));
            }
            
            if (request.getCategory() != null) {
                predicates.add(cb.equal(root.get("category"), request.getCategory()));
            }
            
            return predicates.isEmpty() ? null : cb.and(predicates.toArray(new Predicate[0]));
        };
        
        Page<Article> page = articleRepository.findAll(spec, pageable);
        return PageResponse.of(page.getContent(), page.getTotalElements(), 
                             request.getPage(), request.getSize());
    }
    
    public Article createArticle(ArticleRequest request, User author) {
        Article article = new Article();
        article.setTitle(request.getTitle());
        article.setContent(request.getContent());
        article.setCategory(request.getCategory());
        article.setAuthor(author);
        
        return articleRepository.save(article);
    }
    
    public Article updateArticle(Long id, ArticleRequest request) {
        Article article = getArticleById(id);
        
        article.setTitle(request.getTitle());
        article.setContent(request.getContent());
        article.setCategory(request.getCategory());
        
        return articleRepository.save(article);
    }
    
    public Article getArticleById(Long id) {
        return articleRepository.findById(id)
            .orElseThrow(() -> new RuntimeException("文章不存在"));
    }
    
    public void updateArticleStatus(Long id, ArticleStatus status) {
        Article article = getArticleById(id);
        article.setStatus(status);
        articleRepository.save(article);
    }
    
    public void deleteArticle(Long id) {
        if (!articleRepository.existsById(id)) {
            throw new RuntimeException("文章不存在");
        }
        articleRepository.deleteById(id);
    }
} 